﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace TestWinClient
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmMain : Form
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmMain()
        {
            InitializeComponent();

            Load += new EventHandler(Form1_Load);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Form1_Load(object sender, EventArgs e)
        {
            #region Text
            Text = string.Format("{0} - {1}", Text, AssemblyVersion);

#if DEBUG
            Text = string.Format("{0} - 【Debug】", Text);
#else
            Text = string.Format("{0} - 【Release】", Text);
#endif
            #endregion

            this.textBox1.Text = GetVersionInfo();

            FileInfo fi=new FileInfo(Application.ExecutablePath);
            textBox1.Text += textBox1.Text + Environment.NewLine + Environment.NewLine + fi.LastWriteTime;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetVersionInfo()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(String.Format("关于    ：\t{0}", AssemblyTitle));
            sb.AppendLine(string.Format("产品名称：\t{0}", AssemblyProduct));
            sb.AppendLine(string.Format("版本    ：\t{0}", AssemblyVersion));
            sb.AppendLine(string.Format("版权    ：\t{0}", AssemblyCopyright));
            sb.AppendLine(string.Format("公司名称：\t{0}", AssemblyCompany));
            sb.AppendLine(string.Format("说明    ：\t{0}", AssemblyDescription));

            return sb.ToString();
        }

        #region 程序集属性访问器

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        /// <summary>
        /// 检查更新
        /// </summary>
        private void BtnCheckUpdate_Click(object sender, EventArgs e)
        {
            //UpdaterHelper.RunUpdater();
            //更新检查
            int num = UpdaterHelper.CheckNewFiles();
            if (num <= 0)
            {
                MessageBox.Show("没有找到更新文件，此版本为最新版本。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (DialogResult.Yes == MessageBox.Show(string.Format("在指定网站上找到 {0} 个待更新文件，是否关闭程序马上更新？", num),
                                                    "找到了更新文件！", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                Close();//将关闭,自己重启
                UpdaterHelper.RunUpdater();
            }
        }


    }
}
